package com.engc.common.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;

/**
 * 
 **************************************************************** 
 * 
 <pre>
 * Copyright (c) 2013 –苏州埃涅加克电子信息科技有限公司
 *  Title: SystemFavorite.java
 *  Description: 收藏夹相关功能
 * *************************************************************** 
 *  2013-5-26  V1.0  guzhixiong    New Files for SystemFavorite.java
 * </pre>
 */
@Controller
@RequestMapping("/SystemFavorite")
public class SystemFavorite {
	private BaseService baseService;

	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		try {
			record.put("id", baseService.getSeqValue("SEQ_SYS_FAVORITE"));
			record.put("usercode", BaseUtils.getUserCodeFromSession(request));
			int counter = baseService.executeCountSql("select count(1) from sys_Favorite where usercode=? and menuid=?", new Object[]{record.get("usercode"),record.get("menuid")});
			if(counter>0){
				return BaseUtils.getErrorMessage("该菜单已经添加收藏");
			}
			baseService.Insert("sys_Favorite", record);
			return BaseUtils.getSuccessMessage("添加收藏成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("添加收藏失败，系统发生异常");
		}

	}

	/**
	 * 
	 <pre>
	 * 方法 名 : delete
	 * 功能描述： 删除搜藏
	 * @param request
	 * @return
	 * 创 建 人: guzhixiong
	 * 日    期: 2013-5-26
	 * 修 改 人: 
	 * 日    期:
	 * </pre>
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request) {
		String id = request.getParameter("ID");
		try {
			baseService.Delete("sys_Favorite", "id=?", id);
			return BaseUtils.getSuccessMessage("收藏已移除");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("收藏移除失败，系统发生异常");
		}
	}

	/**
	 * 
	 <pre>
	 * 方法 名 : load
	 * 功能描述： 获取当前登录人所有的收藏夹权限
	 * @param request
	 * @return
	 * 创 建 人: Administrator
	 * 日    期: 2013-5-26
	 * 修 改 人: 
	 * 日    期:
	 * </pre>
	 */
	@RequestMapping("/load")
	@ResponseBody
	public Map<String, Object> load(HttpServletRequest request) {
		List<Map<String, Object>> favorites = baseService
				.Query("select a.id,b.menuname as favoritetitle,a.favoritecontent,b.menuurl as url,b.menuicon as icon,b.menuno as menuno,b.menuid  from sys_Favorite a,sys_menu b where a.menuid = b.menuid and usercode=?",
						BaseUtils.getUserCodeFromSession(request));
		return BaseUtils.getSuccessMessage(favorites);
	}

	/**
	 * 
	 <pre>
	 * 方法 名 : readMessage
	 * 功能描述： 读取未读信息
	 * @param request
	 * @return
	 * 创 建 人:guzhixiong
	 * 日    期: 2013-6-2
	 * 修 改 人: 
	 * 日    期:
	 * </pre>
	 */
	@RequestMapping("/readMessage")
	@ResponseBody
	public Map<String, Object> readMessage(HttpServletRequest request) {
		String _pagenumber = request.getParameter("page");
		String _pagesize = request.getParameter("pagesize");
		int pagenumber = 1, pagesize = 0;
		// 可分页
		if (!BaseUtils.isEmpty(_pagenumber) && !BaseUtils.isEmpty(_pagesize)) {
			pagenumber = Integer.valueOf(_pagenumber);
			pagesize = Integer.valueOf(_pagesize);
			if (pagesize == 0)
				pagesize = 10;
		}
		List<Map<String, Object>> messages = new ArrayList<Map<String, Object>>();
		Object usercode = BaseUtils.getUserCodeFromSession(request);
		int Total = baseService
				.getTotal(
						"select a.ippid,a.msg_title,a.msg_info,a.msg_url,to_char(a.msg_date,'yyyy-mm-dd hh24:mi:ss')as msg_date from stb_ipp_info a ,stb_receiver_info b where a.ippid = b.ippid and a.ipptypeid=3 and b.status<>2 and b.receiver_code = ?",
						usercode);
		if (Total > 0) {
			messages = baseService
					.Query_SliceOracle(
							"select a.ippid,a.msg_title,a.msg_info,a.msg_url,to_char(a.msg_date,'yyyy-mm-dd hh24:mi:ss')as msg_date,a.type from stb_ipp_info a ,stb_receiver_info b where a.ippid = b.ippid and a.ipptypeid=3 and b.status<>2 and b.receiver_code = ? order by a.msg_date desc",
							pagenumber, pagesize, BaseUtils.getUserCodeFromSession(request));
		}
		return BaseUtils.getGridData(Total, messages);
	}

	@Resource
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
